<!-- 报名备注的编辑弹窗 -->
<template>
  <ele-modal
    form
    :width="470"
    :model-value="modelValue"
    title="修改备注"
    @update:modelValue="updateModelValue"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="备注名称" prop="content">
        <el-input clearable v-model="form.content" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
      <el-button
        v-if="isUpdate"
        type="danger"
        :loading="loading"
        @click="remove"
      >
        删除
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import {
    addApplyRecordNote,
    updateApplyRecordNote,
    delApplyRecordNote
  } from '@/api/company';

  const emit = defineEmits(['done', 'update:modelValue']);

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object,
    applyRecordId: Number
  });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    id: 0,
    content: ''
  });

  /** 表单验证规则 */
  const rules = reactive({
    content: [
      {
        required: true,
        message: '请输入备注内容',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value
        ? updateApplyRecordNote
        : addApplyRecordNote;
      var requestData = Object.assign({}, form);
      requestData.apply_record_id = props.applyRecordId;
      saveOrUpdate(requestData)
        .then(() => {
          loading.value = false;
          EleMessage.success('添加成功');
          updateModelValue(false);
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };

  /** 删除单个 */
  const remove = () => {
    ElMessageBox.confirm('确定要删除此备注吗?', '系统提示', {
      type: 'warning',
      draggable: true
    })
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        var requestData = Object.assign({}, form);
        console.log('wtf>');
        delApplyRecordNote(requestData)
          .then(() => {
            loading.close();
            EleMessage.success('删除成功');
            updateModelValue(false);
            emit('done');
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        if (props.data) {
          assignFields({
            ...props.data
          });
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate?.();
      }
    }
  );
</script>
